package org.example.algorithm.str;

public class LongestCommonPrefixSolution {

    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        String res = longestCommonPrefix(strs);
        System.out.println(res);
    }

    public static String longestCommonPrefix(String[] strs) {
        int length = strs[0].length();
        StringBuilder sb = new StringBuilder();
        for (int i=0;i<length;i++) {
            Character compare = strs[0].charAt(i);
            for (int j=1;j<strs.length;j++) {
                if (strs[j].length() <= i || strs[j].charAt(i) != compare) {
                    return sb.toString();
                }
            }
            sb.append(compare);
        }
        return sb.toString();
    }
}
